<?php

namespace App\Admin\Controllers;

use App\Models\Grade;
use App\Http\Controllers\Controller;
use App\Models\Program;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Facades\Admin;
use App\Models\Member;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;

class GradesController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('Index')
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $branch=$this->getBranch();
        $grid = new Grid(new Grade);
        $grid->model()->orderBy('id','desc');
        $grid->actions(function ($actions) {
            $actions->disableView();
        });
        $grid->filter(function($filter){
            $filter->disableIdFilter();
            $filter->like('member.name', '成员');
            $filter->like('program.name', '项目');
            $filter->between('created_at')->datetime();
            $filter->equal('member_id','成员id');
            $filter->equal('program_id','项目id');
        });
        Admin::script('
           $(".fields-group").each(function () {  
                       $(this).children("div").eq(3).hide();
                       $(this).children("div").eq(4).hide();           
                    });
        ');
        $grid->id('Id');
        $grid->column('member.name','成员');
        $grid->column('program.name','所属项目');
//        $grid->column('branch.name','所属部门');
        $grid->time('项目时间跨度');
        $grid->usetime('预估时间');
        $grid->comment('评语');
        $grid->sum('总分');
        $grid->column('score','评级')->display(function (){
         if($this->sum>=90){
             return 'A';
         }elseif ($this->sum>=80){
             return 'B';
         }elseif ($this->sum>=75){
             return 'C';
         }elseif ($this->sum>=60){
             return 'D';
         }elseif($this->sum>0){
             return 'E';
         }
        });
            if($branch[0]=="master") {
                $grid->model()->where('branch_id', $branch[2]);
                $grid->model()->where('plan', 1);
                switch ($branch[2]){
                    case '3':
                        $this->getXy($grid);
                        break;
                    case '4':
                        $this->getGm($grid);
                        break;
                    case '5':
                        $this->getWd($grid);
                        break;
                    case '6':
                        $this->getXy($grid);
                        break;
                }
                $this->oneGrades($grid);
            }elseif ($branch[0]=="member"){
                $grid->actions(function ($actions) {
                    $actions->disableView();
                    $actions->disableDelete();
                });
                $grid->model()->where('member_id', $branch[1]);
                $grid->model()->where('plan', 1);
                switch ($branch[1]){
                    case '3':
                        $this->getXy($grid);
                        break;
                    case '4':
                        $this->getGm($grid);
                        break;
                    case '5':
                        $this->getWd($grid);
                        break;
                    case '6':
                        $this->getXy($grid);
                        break;
                }
            }elseif ($branch[0]=='manager'){
                switch ($branch[1]){
                    case '3':
                        $grid->model()->where('branch_id', $branch[1])->orwhere('branch_id',6);
                        $grid->model()->where('plan', 1);
                        $this->getXy($grid);
                        break;
                    case '4':
                        $grid->model()->where('branch_id', $branch[1]);
                        $grid->model()->where('plan', 1);
                        $this->getGm($grid);
                        break;
                    case '5':
                        $grid->model()->where('branch_id', $branch[1]);
                        $grid->model()->where('plan', 1);
                        $this->getWd($grid);
                        break;
                    case '1':
                        $this->oneGrades($grid);
                        break;
                    default:
                        break;
                }
            }
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Grade::findOrFail($id));

        $show->id('Id');
        $show->gm1('Gm1');
        $show->gm2('Gm2');
        $show->gm3('Gm3');
        $show->gm4('Gm4');
        $show->gm5('Gm5');
        $show->gm6('Gm6');
        $show->gm7('Gm7');
        $show->member_id('Member id');
        $show->module('Module');
        $show->pm('Pm');
        $show->program_id('Program id');
        $show->sum('Sum');
        $show->time('Time');
        $show->wd1('Wd1');
        $show->wd2('Wd2');
        $show->wd3('Wd3');
        $show->wd4('Wd4');
        $show->wd5('Wd5');
        $show->wd6('Wd6');
        $show->wd7('Wd7');
        $show->xy1('Xy1');
        $show->xy2('Xy2');
        $show->xy3('Xy3');
        $show->xy4('Xy4');
        $show->xy5('Xy5');
        $show->xy6('Xy6');

        return $show;
    }
    public function getBranch(){
        $branch="";
        $temp="";
        $mess=$_SERVER['REQUEST_URI'];
        if(Admin::user()->isRole('emei')||Admin::user()->isRole('administrator')){
            if(strpos($mess,'xiaoyao')) {
                return $branch =['manager',3];
            }elseif(strpos($mess,'emei')){
                return $branch =['manager',1];
            }
            elseif(strpos($mess,'gumu')){
                return $branch =['manager',4];
            }elseif(strpos($mess,'wudang')){
                return $branch =['manager',5];
            }elseif (strpos($mess,'member')){
                return $branch =['manager',1];
            }elseif (strpos($mess,'master')){
                return $branch =['manager',5];
            } else{
            return $branch =['member',Admin::user()->id];
            }
        }elseif (Admin::user()->isRole('wdmaster')){
            if(strpos($mess,'xiaoyao')) {
                return $branch =['manager',3];
            }elseif(strpos($mess,'emei')){
                return $branch =['manager',1];
            }
            elseif(strpos($mess,'gumu')){
                return $branch =['manager',4];
            }elseif(strpos($mess,'wudang')){
                return $branch =['manager',5];
            }elseif (strpos($mess,'member')){
                return $branch =['master',1,5];
            }elseif (strpos($mess,'master')){
                return $branch =['manager',5];
            } else{
                return $branch =['member',Admin::user()->id];
            }
        }
        elseif(Admin::user()->isRole('master')){
            $member= Member::all()->toArray();
            foreach ($member as $val){
                if($val['username']==Admin::user()->username){
                    $branch=['master',$val['branch_id']];
                    $temp = ['master',$val['id'],$val['branch_id']];
                    $temp2 = ['member',$val['id'],$val['branch_id']];
                }
            }
            if(strpos($mess,'master')){
                return $branch;
            }elseif (strpos($mess,'member'))
            {
                return $temp;
            } else{
                return $temp2;
            }
        }
        elseif (Admin::user()->isRole('member')){
            $member= Member::all()->toArray();
            foreach ($member as $val){
                if($val['username']==Admin::user()->username){
                    $branch=['member',$val['id'],$val['branch_id']];
                }
            }
            return $branch;
        }
    }
    public function getWd($grid){
        $grid->pro1('项目总结报告(10%)');
        $grid->pro2('项目日报完整性(10%)');
        $grid->pro3('项目计划完整性(20%)');
        $grid->pro4('项目个人技术分享(5%)');
        $grid->pro5('团队沟通协调能力(20%)');
        $grid->pro6('需求跟踪完整性(15%)');
        $grid->pro7('客户满意度(20%)');
    }
    public function getXy($grid){
//        $grid->pm('项目经理');
//        $grid->module('负责模块');
//        $grid->git('版本库地址');
        $grid->pro1('总体时间进度(30%)');
        $grid->pro2('测试与团队配合(25%)');
        $grid->pro3('上线满意度(10%)');
        $grid->pro4('部门内部配合(15%)');
        $grid->pro5('代码规范(10%)');
        $grid->pro6('代码逻辑(10%)');
    }
    public function getGm($grid){
//        $grid->pm('项目经理');
//        $grid->module('负责模块');
        $grid->pro1('总体时间进度(25%)');
        $grid->pro2('跨部素材规范(15%)');
        $grid->pro3('上线满意度(10%)');
        $grid->pro4('部门内部配合(10%)');
        $grid->pro5('作品质量与设计(20%)');
        $grid->pro6('内部时间安排(15%)');
        $grid->pro7('内部素材规范(5%)');
    }
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $branch=$this->getBranch();
        $form = new Form(new Grade);
        $form->tools(function (Form\Tools $tools) {
            $tools->disableList();
            $tools->disableDelete();
            $tools->disableView();
        });
        $form->footer(function ($footer) {
            $footer->disableReset();
            $footer->disableViewCheck();
            $footer->disableEditingCheck();
            $footer->disableCreatingCheck();
        });
        if(is_array($branch)){
            if($branch[0]=='manager'){
                switch ($branch[1]){
                    case '3':
                        $form->select('member_id', '成员')->options(Member::where('state',1)->pluck('name','id'));
                        $form->select('program_id', '项目')->options(Program::all()->pluck('name','id'));
                        $form->text('time', '时间跨度')->disable();
                        $form->text('module', '负责模块')->disable();
                        $form->text('pm', '项目经理')->disable();
                        $form->text('usetime', '此成员预估完成时间')->disable();
                        $form->text('comment', '评语')->disable();
                        $form->text('pro4', '部门内部配合(15%)')->disable();
                        $form->text('pro5', '代码规范(10%)')->disable();
                        $form->text('pro6', '代码逻辑(10%)')->disable();
                        $form->text('pro1', '总体时间进度(30%)')->rules('integer|between:0,30',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro2', '测试与团队配合(25%)')->rules('integer|between:0,25',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro3', '上线满意度(10%)')->rules('integer|between:0,10',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        break;
                    case '4':
                        $form->select('member_id', '成员')->options(Member::where('state',1)->pluck('name','id'));
                        $form->select('program_id', '项目')->options(Program::all()->pluck('name','id'));
                        $form->text('time', '时间跨度')->disable();
                        $form->text('module','负责模块')->disable();
                        $form->text('pm',    '项目经理')->disable();
                        $form->text('usetime', '此成员预估完成时间')->disable();
                        $form->text('comment', '评语')->disable();
                        $form->text('pro4', '部门内部配合(10%)')->disable();
                        $form->text('pro5', '作品质量与设计(20%)')->disable();
                        $form->text('pro6', '内部时间安排(15%)')->disable();
                        $form->text('pro7', '内部素材规范(5%)')->disable();
                        $form->text('pro1', '总体时间进度(25%)')->rules('integer|between:0,25',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro2', '跨部素材规范(15%)')->rules('integer|between:0,15',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro3', '上线满意度(10%)')->rules('integer|between:0,10',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        break;
                    case '5':
                        $form->select('member_id', '成员')->options(Member::where('state',1)->pluck('name','id'));
                        $form->select('program_id', '项目')->options(Program::all()->pluck('name','id'));
                        $form->text('time', '时间跨度');
                        $form->text('usetime', '此成员预估完成时间');
                        $form->text('comment', '评语');
                        $form->text('pro1', '项目总结报告(10%)')->rules('integer|between:0,10',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro2', '项目日报完整性(10%)')->rules('integer|between:0,10',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro3', '项目计划完整性(20%)')->rules('integer|between:0,20',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro4', '项目个人技术分享(5%)')->rules('integer|between:0,5',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro5', '团队沟通协调能力(20%)')->rules('integer|between:0,20',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro6', '需求跟踪完整性(15%)')->rules('integer|between:0,15',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro7', '客户满意度(20%)')->rules('integer|between:0,20',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        break;
                    case '6':
                        $form->select('member_id', '成员')->options(Member::where('state',1)->pluck('name','id'));
                        $form->select('program_id', '项目')->options(Program::all()->pluck('name','id'));
                        $form->text('time', '时间跨度')->disable();
                        $form->text('module', '负责模块')->disable();
                        $form->text('pm', '项目经理')->disable();
                        $form->text('usetime', '此成员预估完成时间')->disable();
                        $form->text('comment', '评语')->disable();
                        $form->text('pro4', '部门内部配合(15%)')->disable();
                        $form->text('pro5', '代码规范(10%)')->disable();
                        $form->text('pro6', '代码逻辑(10%)')->disable();
                        $form->text('pro1', '总体时间进度(30%)')->rules('integer|between:0,30',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro2', '测试与团队配合(25%)')->rules('integer|between:0,25',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro3', '上线满意度(10%)')->rules('integer|between:0,10',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        break;
                    default:
                        break;
                }

            } elseif($branch[0]=='master') {
                switch ($branch[2]){
                    case '3':
                        $form->select('member_id', '成员')->options(Member::where('state',1)->pluck('name','id'));
                        $form->select('program_id', '项目')->options(Program::all()->pluck('name','id'));
                        $form->text('time', '时间跨度')->disable();
                        $form->text('module', '负责模块')->disable();
                        $form->text('pm', '项目经理')->disable();
                        $form->text('usetime', '此成员预估完成时间');
                        $form->text('comment', '评语');
                        $form->text('pro4', '部门内部配合(15%)')->rules('integer|between:0,15',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro5', '代码规范(10%)')->rules('integer|between:0,10',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro6', '代码逻辑(10%)')->rules('integer|between:0,10',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro1', '总体时间进度(30%)')->disable();
                        $form->text('pro2', '测试与团队配合(25%)')->disable();
                        $form->text('pro3', '上线满意度(10%)')->disable();
                        break;
                    case '4':
                        $form->select('member_id', '成员')->options(Member::where('state',1)->pluck('name','id'));
                        $form->select('program_id', '项目')->options(Program::all()->pluck('name','id'));
                        $form->text('time', '时间跨度')->disable();
                        $form->text('module','负责模块')->disable();
                        $form->text('pm',    '项目经理')->disable();
                        $form->text('usetime', '此成员预估完成时间');
                        $form->text('comment', '评语');
                        $form->text('pro4', '部门内部配合(10%)')->rules('integer|between:0,10',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro5', '作品质量与设计(20%)')->rules('integer|between:0,20',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro6', '内部时间安排(15%)')->rules('integer|between:0,15',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro7', '内部素材规范(5%)')->rules('integer|between:0,5',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro1', '总体时间进度(25%)')->disable();
                        $form->text('pro2', '跨部素材规范(15%)')->disable();
                        $form->text('pro3', '上线满意度(10%)')->disable();
                        break;
                    case '5':
                        $form->select('member_id', '成员')->options(Member::where('state',1)->pluck('name','id'));
                        $form->select('program_id', '项目')->options(Program::all()->pluck('name','id'));
                        $form->text('time', '时间跨度');
                        $form->text('usetime', '此成员预估完成时间');
                        $form->text('comment', '评语');
                        $form->text('pro1', '项目总结报告(10%)')->rules('integer|between:0,10',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro2', '项目日报完整性(10%)')->rules('integer|between:0,10',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro3', '项目计划完整性(20%)')->rules('integer|between:0,20',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro4', '项目个人技术分享(5%)')->rules('integer|between:0,5',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro5', '团队沟通协调能力(20%)')->rules('integer|between:0,20',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro6', '需求跟踪完整性(15%)')->rules('integer|between:0,15',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro7', '客户满意度(20%)')->rules('integer|between:0,20',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        break;
                    case '6':
                        $form->select('member_id', '成员')->options(Member::where('state',1)->pluck('name','id'));
                        $form->select('program_id', '项目')->options(Program::all()->pluck('name','id'));
                        $form->text('time', '时间跨度')->disable();
                        $form->text('module', '负责模块')->disable();
                        $form->text('pm', '项目经理')->disable();
                        $form->text('usetime', '此成员预估完成时间');
                        $form->text('comment', '评语');
                        $form->text('pro4', '部门内部配合(15%)')->rules('integer|between:0,15',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro5', '代码规范(10%)')->rules('integer|between:0,10',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro6', '代码逻辑(10%)')->rules('integer|between:0,10',['integer'=>'请输入数字','between'=>'评分不能超过百分比']);
                        $form->text('pro1', '总体时间进度(30%)')->disable();
                        $form->text('pro2', '测试与团队配合(25%)')->disable();
                        $form->text('pro3', '上线满意度(10%)')->disable();
                     break;
                    default:
                        break;
                }
            }else{
                $form->text('pm', '项目经理');
                $form->text('module', '负责模块');
                $form->textarea('git', '版本库地址');
            }

        }
        return $form;
    }
    public function oneGrades($grid){
        $mess=$_SERVER['REQUEST_URI'];
        if(strpos($mess,'member_id')){
            $grid->footer(function ($query) {
                $data = $query->get();
                $arr=[];
                $a=$b=$c=$d=$e=0;
                foreach ($data as $value){
                    array_push($arr,$value['sum']);
                    if($value['sum']>=90){
                        $a++;
                    }elseif ($value['sum']>=80){
                        $b++;
                    }elseif ($value['sum']>=75){
                        $c++;
                    }elseif ($value['sum']>=60){
                        $d++;
                    }elseif($value['sum']>0){
                        $e++;
                    }
                }
                $one= ['所得绩效',$a, $b,$c,$d,$e];
                if($c>0){
                    if($b>=2){
                        $b-=2;
                        $c--;
                    } elseif($a>=$c){
                        $a--;
                        $c--;
                    }
                }
                if($d>0){
                    if($b>=5){
                        $b-=5;
                        $d--;
                    }elseif($a==1&&$b>=3){
                        $a--;
                        $d--;
                        $b-=3;
                    }elseif($a>=2){
                        $a-=2;
                        $d--;
                    }
                }

                $two=['抵消后绩效',$a, $b,$c,$d,$e];
                $headers = ['绩效评级','A', 'B', 'C','D','E'];
                $rows = [
                    $one,$two
                ];
                $table = new Table($headers, $rows);
                return $table;
            });
        }
    }
}
